package cn.lxw.androiddeamon.service

import android.annotation.SuppressLint
import android.app.Notification
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder

/**
 * 源代码： Lxw
 * E-mail：china2021@126.com
 * 时间轴： 2018-03-09 下午10:54
 *
 * 利用系统漏洞，启动第二
 */
class ForegroundService : Service() {

    /** 通知ID*/
    private val NOTIFICATION_ID = 7

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }


    @SuppressLint("ObsoleteSdkInt")
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            startForeground(NOTIFICATION_ID, Notification())
        } else {
            startForeground(NOTIFICATION_ID, Notification())
            startService(Intent(this, InnerService::class.java))
        }

        return super.onStartCommand(intent, flags, startId)

    }


    /**
     * 系统内部服务，绑定相同的通知ID之后自己关闭掉。
     */
     class  InnerService : Service() {
        override fun onBind(intent: Intent?): IBinder? {
            return null
        }

        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
            startForeground(7, Notification())
            stopForeground(true)
            stopSelf()
            return super.onStartCommand(intent, flags, startId)
        }
    }
}